package com.lizhenhua.api.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lizhenhua.api.feignClient.QueryUserClient;
import com.lizhenhua.api.feignClient.RegistUserClient;
import com.lizhenhua.api.service.UserRegistService;
import com.lizhenhua.fmmall.beans.Users;
import com.lizhenhua.fmmall.utils.MD5Utils;
import com.lizhenhua.fmmall.vo.ResStatus;
import com.lizhenhua.fmmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class UserRegistServiceImpl implements UserRegistService {

    @Autowired
    private RegistUserClient registUserClient;

    @Autowired
    private QueryUserClient queryUserClient;

    @Autowired
    private ObjectMapper o;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //    @Transactional // 定义成一个事务
//    public ResultVo userResgit(String userName, String password) {
//
//        //synchronized  线程锁  锁UserServiceImpl 这个类  必须是同一个线程才是同一个对象 这个锁才生效
//        synchronized (this) {
//            /**
//             * 1. 根据用户查询，这个用户是否已经被注册
//             */
//            // 此处的Example应该是属于 tk.mybatis的
//            Example example = new Example(Users.class);
//
//            Example.Criteria criteria = example.createCriteria();
//            criteria.andEqualTo("username", userName);
//
//            List<com.lizhenhua.fmmall.entity.Users> usersList = userMapper.selectByExample(example);
//            com.lizhenhua.fmmall.entity.Users users;
//
//            if (usersList.size() == 0) {
//// MD5 工具类加密密码 返回类型为String类型
//                MD5Utils.md5(password);
//                users = new com.lizhenhua.fmmall.entity.Users();
//                users.setUsername(userName);
//                users.setPassword(MD5Utils.md5(password));
//                users.setUserImg("img/default.png");
//                users.setUserRegtime(new Date());
//                users.setUserModtime(new Date());
//                int i = userMapper.insert(users);
//                /**
//                 * 2.如果没有进行注册 则进行保存操作
//                 */
//
//                if (i > 0) {
//                    return new ResultVo(ResStatus.OK, "注册成功", users);
//                } else {
//                    return new ResultVo(ResStatus.NO, "注册失败", null);
//
//                }
//
//            } else {
//                return new ResultVo(ResStatus.NO, "用户名已经被注册", null);
//            }
//        }
//    }

    @Transactional // 定义成一个事务
    public ResultVo userResgit(String username, String pwd) {
        synchronized (this) {
            Users users = queryUserClient.checkUser(username);
            if (users == null) {
                MD5Utils.md5(pwd);
                users = new Users();
                users.setUsername(username);
                users.setPassword(MD5Utils.md5(pwd));
                users.setUserImg("img/default.png");
                users.setUserRegtime(new Date());
                users.setUserModtime(new Date());
                int i = registUserClient.save(users);

                if (i > 0) {
                    return new ResultVo(ResStatus.OK, "注册成功", users);
                } else {
                    return new ResultVo(ResStatus.NO, "注册失败", null);
                }
            } else if (users.getUsername() == null) {
                return new ResultVo(ResStatus.NO, "网络出现故障请重试", null);
            } else {
                return new ResultVo(ResStatus.NO, "该户用命已经被注册", null);
            }
        }

    }


}